package edu.uts.reekie.designer.implementation.edit;

import java.util.List;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.EllipseAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.AccessibleEditPart;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.swt.accessibility.AccessibleControlEvent;
import org.eclipse.swt.accessibility.AccessibleEvent;

import edu.uts.reekie.designer.Messages;
import edu.uts.reekie.designer.implementation.figures.*;
import edu.uts.reekie.designer.implementation.model.*;
import edu.uts.reekie.designer.edit.*;
import edu.uts.reekie.designer.figures.FigureFactory;
import edu.uts.reekie.designer.model.ModelFactory;

public class ViewExternalInterfaceEditPart extends ViewNodeEditPart {

	public ViewExternalInterfaceEditPart(ModelFactory mf, FigureFactory ff) {
		super(mf, ff);
	}


	protected IFigure createFigure() {
		return new ViewExternalInterfaceFigure(getViewExternalIntefaceModel().getName());
	}
	
	
	protected ViewExternalInterfaceFigure getViewExternalIntefaceFigure() {
		return (ViewExternalInterfaceFigure)getFigure();
	}
	
	protected ViewExternalInterface getViewExternalIntefaceModel() {
		return (ViewExternalInterface)getModel();
	}

	protected void createEditPolicies(){
		super.createEditPolicies();
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new NamedDirectEditPolicy());
	}
	
	public void performRequest(Request request){
		if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
			performDirectEdit();
	}
	
	private void performDirectEdit(){
		
		new NameEditManager(this,
				new NameCellEditorLocator((Figure)getFigure())).show();
	}
	
	protected ConnectionAnchor getConnectionAnchor() {
		return getViewExternalIntefaceFigure().getAnchor();
	}
	
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections()
	 */
	protected List getModelSourceConnections() {
		return getViewExternalIntefaceModel().getSourceConnections();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections()
	 */
	protected List getModelTargetConnections() {
		return getViewExternalIntefaceModel().getTargetConnections();
	}

	protected AccessibleEditPart createAccessible() {
		// TODO: implement properly, rather than just copying from LED
		return new AccessibleGraphicalEditPart(){

			public void getName(AccessibleEvent e) {
				e.result = Messages.LogicPlugin_Tool_CreationTool_LED_Label;
			}
			
			public void getValue(AccessibleControlEvent e) {
				e.result = "dummy value"; // TODO: implement properly
			}

		};
	}

}
